﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="ToolStrip.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 9923                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-05-10 14:48:29 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    #endregion

    public abstract class ToolStrip : ScrollableWidget
    {
        #region Private Members
        private ToolStripItemCollection items;
        #endregion

        #region Constructors
        public ToolStrip(GuiManager manager, string name)
            : base(manager, name)
        {
            this.Init();
        }

        public ToolStrip(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
            this.Init();
        }
        #endregion

        public ToolStripItemCollection Items
        {
            get
            {
                return this.items;
            }
        }

        public override void Draw(GameTime gameTime, IGuiRenderer renderer)
        {
            base.Draw(gameTime, renderer);

            Vector2 itemPosition = new Vector2(ClientRectangle.X, ClientRectangle.Y);
            foreach (ToolStripItem tsmi in this.Items)
            {
                tsmi.Draw(gameTime, renderer, this, ref itemPosition);
                itemPosition.X += Layout.GetProperty("ItemSpacing").FloatValue;
            }
        }

        public override void OnMouseMove(object sender, MouseEventArgs e)
        {
            RectangleF itemRect = RectangleF.Empty;
            foreach (ToolStripItem tsmi in this.Items)
            {
                itemRect = tsmi.GetHitTestRectangle();

                if (itemRect.Contains(e.Position))
                {
                    tsmi.Focus = true;
                }
                else
                {
                    if (tsmi is ToolStripDropDownItem)
                    {
                        foreach (ToolStripItem subItem in ((ToolStripDropDownItem)tsmi).DropDownItems)
                        {
                            if (subItem.GetHitTestRectangle().Contains(e.Position))
                            {
                                tsmi.Focus = true;
                                break;
                            }
                        }

                        if (tsmi.Focus)
                        {
                            continue;
                        }
                    }

                    tsmi.Focus = false;
                }
            }

            base.OnMouseMove(sender, e);
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.LeftButton)
            {
                foreach (ToolStripItem tsi in this.Items)
                {
                    if (tsi.Focus)
                    {
                        tsi.OnClick(this, EventArgs.Empty);
                    }
                }
            }

            base.OnMouseUp(sender, e);
        }

        public override void OnLostFocus(object sender, EventArgs e)
        {
            foreach (ToolStripItem tsi in this.Items)
            {
                tsi.Focus = false;
            }

            base.OnLostFocus(sender, e);
        }

        private void Init()
        {
            this.items = new ToolStripItemCollection(this);
        }
    }
}
